package com.flower.controller.admin;
import com.flower.service.DataAnalysisService;
import com.flower.utils.AjaxResult;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;

/**
 * 商城数据分析
 */
@RestController
@RequestMapping("/admin/analysis")
@RequiredArgsConstructor
public class DataAnalysisController {

    private final DataAnalysisService dataAnalysisService;

    /**
     * 获取销售概览数据
     */
    @GetMapping("/sales/overview")
    public AjaxResult getSalesOverview() {
        return AjaxResult.success(dataAnalysisService.getSalesOverview());
    }

    /**
     * 获取销售趋势数据
     * @param startDate 开始日期
     * @param endDate 结束日期
     * @param timeUnit 时间单位：day, week, month
     */
    @GetMapping("/sales/trend")
    public AjaxResult getSalesTrend(
            @RequestParam(required = false) LocalDate startDate,
            @RequestParam(required = false) LocalDate endDate,
            @RequestParam(defaultValue = "day") String timeUnit) {
        return AjaxResult.success(dataAnalysisService.getSalesTrend(startDate, endDate, timeUnit));
    }

    /**
     * 获取订单状态分布
     */
    @GetMapping("/orders/status")
    public AjaxResult getOrderStatusDistribution() {
        return AjaxResult.success(dataAnalysisService.getOrderStatusDistribution());
    }

    /**
     * 获取热销商品排行
     * @param limit 返回数量限制
     */
    @GetMapping("/products/top")
    public AjaxResult getTopProducts(@RequestParam(defaultValue = "10") Integer limit) {
        return AjaxResult.success(dataAnalysisService.getTopProducts(limit));
    }

    /**
     * 获取店铺销售排行
     * @param limit 返回数量限制
     */
    @GetMapping("/shops/top")
    public AjaxResult getTopShops(@RequestParam(defaultValue = "10") Integer limit) {
        return AjaxResult.success(dataAnalysisService.getTopShops(limit));
    }

    /**
     * 获取商品分类销售分布
     */
    @GetMapping("/categories/distribution")
    public AjaxResult getCategoryDistribution() {
        return AjaxResult.success(dataAnalysisService.getCategoryDistribution());
    }
} 